/**   
 * @Title: ImageListCache.java 
 * @Package com.esun.util.img 
 * @Description: 缓存队列中的图片 
 * @author: Miko baoz@500wan.com  
 * @date: 2011-9-9 下午04:19:17  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.util.img;

import java.util.ArrayList;

import com.esun.config.Config;
import com.esun.util.img.db.ImageDB;
import com.esun.util.log.EDebug;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

/**
 * @ClassName: ImageListCache
 * @Description: 缓存队列中的图片
 * @author: Miko baoz@500wan.com
 * @date: 2011-9-9 下午04:19:17
 * 
 */
public class ImageListCache {

	private ArrayList<Object[]> alDatas = new ArrayList<Object[]>();

	private Handler cacheHandler;

	private static final int SUCCESS = 10000;
	private static final int FAILED = 10001;

	private static final int RETRY_TIMES = 3;

	private Context context;

	private boolean isClose = false;

	public ImageListCache(Context context, ArrayList<Object[]> line) {

		for (int i = 0; i < line.size(); i++) {
			alDatas.add(line.get(i));
		}

		this.context = context;

		initHandler();
		startWorking();
	}

	public void addToLine(ArrayList<Object[]> alAdding) {
		if (!isClose) {
			alDatas.addAll(alAdding);
		} else {
			start();
		}
	}

	public void stop() {
		EDebug.o(this, "imge list catch thread stop");
		isClose = true;
	}

	public void cleanDatas() {
		EDebug.o(this, "imge list catch clear");
		alDatas.clear();
	}

	public void start() {
		isClose = false;
		startWorking();
	}

	private void initHandler() {
		cacheHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				switch (msg.what) {
				case SUCCESS:

					Object[] obj = (Object[]) msg.obj;
					showImage((Bitmap) obj[0], (ImageView) obj[1]);
					break;

				case FAILED:
					break;

				default:
					break;
				}
			}
		};
	}

	private void showImage(Bitmap bmp, ImageView imgView) {

		EDebug.o(this, "now showing image & the imageView Tag is [" + imgView.getTag() + "]");

		imgView.setImageBitmap(bmp);
	}

	private void startWorking() {
		new Thread() {

			int retryCount = 0;
			ArrayList<Object> alSuccessed = new ArrayList<Object>();

			@Override
			public void run() {
				super.run();

				ImageDB.getImageDB().open(context);

				while (!isClose) {
					while (alDatas.size() > 0 && retryCount < RETRY_TIMES) {

						if (isClose) {
							EDebug.o(ImageListCache.class, " Tread death !!!");
							return;
						}

						EDebug.o(ImageListCache.class, alDatas.size() + " Beginning");

						for (int i = 0; i < alDatas.size(); i++) {

							EDebug.o(ImageListCache.class, " ****  ItemNumber " + i);

							if (isClose) {
								EDebug.o(ImageListCache.class, " Tread death !!!");
								return;
							}

							Object[] obj = alDatas.get(i);

							String imgName = String.valueOf(obj[0]);

							if (!ImageDB.getImageDB().dbIsOpen()) {
								ImageDB.getImageDB().open(context);
							}

							Bitmap img = ImageUtils.getLocalImg(imgName);

							if (img != null) {

								alSuccessed.add(obj);

								Object[] objSending = { img, obj[1] };

								Message msg = cacheHandler.obtainMessage(SUCCESS, objSending);
								cacheHandler.sendMessage(msg);

							} else {

								img = ImageUtils.downLoadImg(imgName, Config.SPORTTERYFOOTBALL_PIC_ADDRESS);

								if (img != null) {

									alSuccessed.add(obj);

									Object[] objSending = { img, obj[1] };

									Message msg = cacheHandler.obtainMessage(SUCCESS, objSending);
									cacheHandler.sendMessage(msg);

									ImageUtils.saveImg(imgName, img);

								} else {
									retryCount++;
								}
							}

							try {
								EDebug.o(ImageListCache.class, " Tread Sleep 0.5 second !!!【1】");
								sleep(500);
							} catch (Exception e) {
								EDebug.o(ImageListCache.class, "Error");
							}
						}

						// EDebug.o(ImageListCache.class, alDatas.size() +
						// " alSuccessed size!");
						for (int i = 0; i < alSuccessed.size(); i++) {
							alDatas.remove(alSuccessed.get(i));
						}

						EDebug.o(ImageListCache.class, alDatas.size() + " Rest!");

						if (alDatas.size() <= 0) {
							// isClose = true;
							EDebug.o(ImageListCache.class, " Tread Going on !!!");
							break;
						}
					}

					if (isClose) {
						EDebug.o(ImageListCache.class, " Tread death !!!");
						return;
					} else {
						try {
							EDebug.o(ImageListCache.class, " Tread Sleep 0.5 second !!!【2】");
							sleep(500);
						} catch (Exception e) {
							EDebug.o(ImageListCache.class, "Error");
						}
					}
				}
				ImageDB.getImageDB().close();
			}
		}.start();
	}
}
